#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 30, M = 310;

int n, m;
char offset = 'A';
int tr[M][3], fail[M], val[M], idx;
int f[1010][M];

void insert(const string& s) {
    int p = 0;
    for (char it : s) {
        int c = it - 'A';
        if (tr[p][c] == 0) tr[p][c] = ++idx;
        p = tr[p][c];
    }
    val[p]++;
}

void setfail() {
    queue<int> q;
    for (int i = 0; i < 3; i++)
        if (tr[0][i]) q.push(tr[0][i]);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (int i = 0; i < 3; i++) {
            if (tr[u][i]) {
                int v = tr[u][i];
                fail[v] = tr[fail[u]][i];
                val[v] += val[fail[v]]; 
                q.push(v);
            } else {
                tr[u][i] = tr[fail[u]][i]; 
            }
        }
    }
}

void solve() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        string s;
        cin >> s;
        insert(s);
    }
    setfail();

    memset(f, -1, sizeof f); 
    f[0][0] = 0;

    for (int i = 0; i < m; i++) {
        for (int j = 0; j <= idx; j++) {
            if (f[i][j] == -1) continue;
            for (int c = 0; c < 3; c++) {
                int nxt = tr[j][c];
                f[i + 1][nxt] = max(f[i + 1][nxt], f[i][j] + val[nxt]);
            }
        }
    }

    int ans = 0;
    for (int j = 0; j <= idx; j++)
        ans = max(ans, f[m][j]);

    cout << ans << '\n';
}


signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    //cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}